package com.wontube.smr.web.controller;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.remote.Remoting;
import com.wontube.smr.web.client.IViewHistoryHandler;
import com.wontube.smr.web.client.model.po.ViewHistory;
import com.wontube.web.verify.VerifyResult;

/**
 * @author tanggf(2012-5-30)
 *
 */
@Controller
public class ViewHistoryController {
	private final static String SUCCESS = "Success";
	private final static String FAILURE = "Failure: Something wrong";
	private final static String VIEW_COOKIE_KEY = "view";
	private final static int LIMIT = 27;
	
	@RequestMapping(value="/view/history/update", method=RequestMethod.POST)
	@ResponseBody
	public String add_or_update_view_history(ViewHistory history,  
			HttpServletRequest request, HttpServletResponse response){		
		VerifyResult verify = history.verify();
		if( ! verify.isDone()){
			return verify.getErrorMessages();
		}
		
		String session = getOrNewSession(request, response);
		history.setSession(session);
		IViewHistoryHandler handler = getHandler();
		Integer aff = handler.addOrUpdateViewHistory(history);
		
		if(aff != null && aff == 1){
			return SUCCESS;
		}else{
			return FAILURE;
		}		
	}
	
	@RequestMapping(value="/view/history/delete", method=RequestMethod.POST)
	@ResponseBody
	public String delete_view_history(@RequestParam("id") String id,
			HttpServletRequest request){
		String session = getSession(request);
		if(session != null){
			IViewHistoryHandler handler = getHandler();
			Integer aff = 0;
			if("ALL".equalsIgnoreCase(id)){
				aff = handler.deleteAllViewHistories(session);
			}else{
				aff = handler.deleteViewHistory(session, id);
			}			
			if(aff != null && aff >= 1){
				return SUCCESS;
			}
		}
		return FAILURE;
	}
	
	@RequestMapping("/view/history/list")
	public ModelAndView list_view_histories(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("home/view_history_list");
		String session = getSession(request);
		if(session != null){
			IViewHistoryHandler handler = getHandler();
			List<ViewHistory> histories = handler.listLatestViewHistories(session, LIMIT);
			mv.addObject("histories", histories);
		}
		return mv;
	}
	
	private String getOrNewSession(HttpServletRequest request, HttpServletResponse response) {
		String session = getSession(request);		
		if(session == null){
			//new cookie
			session = UUID.randomUUID().toString();
			Cookie cookie = new Cookie(VIEW_COOKIE_KEY, session);
			cookie.setPath("/");
			cookie.setMaxAge(3600 * 24 * 1000);
			response.addCookie(cookie);
		}
		
		return session;
	}
	
	private String getSession(HttpServletRequest request){
		String session = null;
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			for (Cookie cookie : cookies) {
				if(VIEW_COOKIE_KEY.equals(cookie.getName())){
					session = cookie.getValue();
					break;
				}
			}
		}
		
		return session;
	}

	private IViewHistoryHandler getHandler(){
		Remoting<IViewHistoryHandler> remoting = new Remoting<IViewHistoryHandler>();	
		return remoting.createService(IViewHistoryHandler.class);
	}
}
